Skip to main content

Variables

Try PowerShell - One man's constant is another man's variable.

Variable, in terms of computer programming, is a place holder in the Memory to store some values. Variables in PowerShell can store any kind of value (data type). They can be String, Integer, Date and time, WMI Object etc. When you interact with the PowerShell Console you normally don’t have to worry about anything. However, when you start scripting you will need to look into what type of data values you want in those variables.

$

Simply use a dollar sign $ to declare a variable. Variable names can contain underscores, letters, numbers and spaces. They don’t persist. Once the script or the shell console exits, they are removed from the Memory.

$Var1 = 100
Write-Output $Var1
${Var2 With Space} = "This variable name has spaces."
Write-Output ${Var2 With Space}

# Alternatively, you can use the cmdlet below to declare a variable.
New-Variable -Name Var3 -Value 200
Write-Output $Var3

All of the above variables are declared as type-implicit variables. PowerShell determines what type it is, depending on what goes in there. However, when you want your script to accept only certain values, you will need to explicitly define what those values should be.
Type explicit variables are declared as below. Just put the data type or object type in square brackets before the variable. For example, the variable $Var4 can only contain Integer values and $Var5 String values.

[Int]$Var4 = 400
[String]$Var5 = "This is a string variable"

Type casting

One key distinction to note is that you can apply the type either to the variable or to the value. When you apply the type to the value, it is called type casting.

$a = [Int]"99"
$a = [String]"This is now a string."
[Int]$b = "99"
$b = "This will throw an error."

You can also predefine the values that can go into a variable by using ValidateSet attribute as below. PowerShell will accept only the values defined in the set.

[ValidateSet("a","y","n")][Char]$Answer = "y"
$Answer = "test" #This will throw an error

Array

An array is a data structure where you can store multiple values. In PowerShell, empty arrays can be declared as below and assign them with some values.

# Declaring arry
[Array]$col1
$col2 = @()
# Assigning values
$col1 = "a", "b", "c", "1", "2", "3"
$col2 = 1, 2, 3, 4, 5, 6

Once the values are assigned to the array, items are accessed by their index values. For example

$col[0] # The item value is a.
$col[2] # The item value is 3.
$col1.IndexOf("f") # The index location for the value f is 5.

Note that the index always begins from 0 (NOT from 1).

# Integer array
[Int[]]$MyIntArray = 2,3,4,5,6

#String array
[String[]]$MyStrArray = 'Orange', 'Apple', 'Banana', 'Kiwi'

Below is an example of using a Byte array data type to store a picture file and write it back onto the disk with a different name. You could literally copy the jpeg file with a different name to a different location using Copy-Item cmdlet. But the purpose of the demonstration is to show you how it can be done using the byte data type. This is useful for the scenario where you have to copy the photo back to Active Directory photo thumbnail area.

[Byte[]]$Photo = Get-Content .\Pictures\my.cat.jpg -Encoding byte
$Photo | Set-Content my-cat.jpg -Encoding byte

Constant

As the name suggests, once the constant variable is declared, you can not change the value of it.

# The value of Pi in Mathematics
New-Variable -Name Pi -Value 3.14285714285714 -Option Constant
Write-Output $Pi

There is also a 'ReadOnly' option if you need to make the variable 'read only'. You must use the Force switch to change the value of it.

# Read Only variable
New-Variable -Name ReadMe -Value "This is a readonly variable" -Option ReadOnly
Write-Output $ReadMe
Set-Variable -Name ReadMe -Value "This is changed by force!" -Force